<?php
using("Db_Adodb_PreparedStatement");
get(Libs_Catalog::fetch("adodb/adodb.inc"));

/**
 * Note that this test assumes that there is more than
 * one row in the CustomAttributes table in order to
 * avoid passing on a failed connection
 */
class Tests_Db_Adodb_PreparedStatement extends UnitTestCase {

	/**
	 * Adodb Connection
	 *
	 * @var ADOConnection
	 */
	private $_dbh;
	
	private $_query = "SELECT * FROM CustomAttributes";
	
	private $_query1 = "SELECT * FROM CustomAttributes WHERE key = ?";
	
	private $_query2 = "INSERT INTO CustomAttributes (key, value) VALUES ('foo', 'bar')";
	
	private $_query3 = "INSERT INTO CustomAttributes (key, value) VALUES (?, ?)";
	
	private $_query4 = "INSERT CustomAttributes () VALUES ( ? , ?')";
	
	public function __construct() {
		$this->_dbh = ADONewConnection('pdo');
		$this->_dbh->Connect("sqlite:" . Tests_Databases_Catalog::fetch("test.db"));
	}
	
	public function testExecuteSelect() {
		$stmt = new Db_Adodb_PreparedStatement($this->_dbh, $this->_query);
		$rows = $stmt->executeSelect();
		$results = array();
		foreach ($rows as $row) {
			$results[] = $row;
		}
		$this->assertTrue(count($results) > 1);
	}
	
	public function testExecuteSelect1() {
		$stmt = new Db_Adodb_PreparedStatement($this->_dbh, $this->_query1);
		$rows = $stmt->executeSelect(array("foo"));
		$results = array();
		foreach ($rows as $row) {
			$results[] = $row;
		}
		$this->assertTrue(count($results) > 1);
	}
	
	public function testExecuteSave() {
		$stmt = new Db_Adodb_PreparedStatement($this->_dbh, $this->_query2);
		$this->assertTrue($stmt->executeSave());
	}
	
	public function testExecuteSave1() {
		$stmt = new Db_Adodb_PreparedStatement($this->_dbh, $this->_query3);
		$this->assertTrue($stmt->executeSave(array('foo', 'bar')));
	}
	
	public function testExceptions() {
		$stmt = new Db_Adodb_PreparedStatement($this->_dbh, $this->_query4);
		try {
			$stmt->executeSave(array('foo', 'bar'));
			$this->fail("Failed to catch Db_Common_SqlException exception");
		} catch (Db_Common_SqlException $e) {
			$this->pass("Caught Db_Common_SqlException exception");
		}
		
		try {
			$stmt->executeSelect(array('foo'));
			$this->fail("Failed to catch Db_Common_SqlException exception");
		} catch (Db_Common_SqlException $e) {
			$this->pass("Caught Db_Common_SqlException exception");
		}
	}
	
	
}
?>